﻿using System.Windows.Forms;

namespace AMatysiak.Controls
{
    public class DataGridViewBoolCell : DataGridViewCheckBoxCell
    {
        private bool value;
        public new bool Value
        {
            get { return value; }
            set 
            { 
                this.value = value;
                setCheckBoxValue();
            }
        }

        private void setCheckBoxValue()
        {
            if (newValue != Value)
            {
                DataGridViewCellEventArgs e = 
                    new DataGridViewCellEventArgs(this.ColumnIndex, this.RowIndex);
                raiseContentClick(e);
            }
        }

        private void raiseContentClick(DataGridViewCellEventArgs e)
        {
            newValue = !newValue;
            base.OnContentClick(e);
            value = newValue;
        }

        public DataGridViewBoolCell() : base(false)
        {
            value = false;
        }

        private bool forced = false;
        private bool newValue = false;

        protected override void OnContentClick(DataGridViewCellEventArgs e)
        {
            if (forced)
                raiseContentClick(e);
        }

        protected override void OnClick(DataGridViewCellEventArgs e)
        {
            forced = true;
            OnContentClick(e);
            forced = false;
        }
    }
}
